'use strict';
const Controller = require('egg').Controller;
const exportExcel = require('../../utils/exportExcel');

function toInt(str) {
  if (typeof str === 'number') return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class ContractController extends Controller {

  async create() {
    const ctx = this.ctx;
    const params = ctx.request.body;
    console.log(params);
    const data = await ctx.model.Contract.bulkCreate(params);
    ctx.body = {
      code: 0,
      data,
      message: '创建成功',
    };
  }

  async index() {
    const { ctx } = this;
    const { company, type, amount, mark, time, pageSize = 10, current = 1 } = ctx.query;
    const limit = Number(pageSize);
    const offset = limit * ((current || 1) - 1);
    const query = {};
    if (company) { query.where = { ...query.where, company }; }
    if (type) { query.where = { ...query.where, type }; }
    if (amount) { query.where = { ...query.where, amount }; }
    if (mark) { query.where = { ...query.where, mark }; }
    if (time) { query.where = { ...query.where, time }; }
    const data = await ctx.model.Contract.findAll({ ...query, limit, offset });
    const totalData = await ctx.model.Contract.findAndCountAll();
    ctx.body = {
      code: 0,
      data,
      pageInfo: {
        total: totalData.count,
        current,
        pageSize: Number(pageSize),
      },
      message: 'success',
    };
  }

  async show() {
    const { ctx } = this;
    const detail = await ctx.model.Contract.findByPk(ctx.params.id, { raw: true });
    ctx.body = {
      code: 0,
      message: 'success',
      data: detail,
    };
  }

  async update() {
    const ctx = this.ctx;
    const id = toInt(ctx.params.id);
    const delivery = await ctx.model.Contract.findByPk(id);
    if (!delivery) {
      ctx.body = {
        code: 1,
        data: [],
        message: '不存在的快递',
      };
      return;
    }
    const params = ctx.request.body;
    const data = await delivery.update(params);
    ctx.body = {
      code: 0,
      data,
      message: '更新成功',
    };
  }

  async download() {
    const { ctx } = this;
    const { company, type, amount, mark, time } = ctx.query;
    const query = {};
    if (company) { query.where = { ...query.where, company }; }
    if (type) { query.where = { ...query.where, type }; }
    if (amount) { query.where = { ...query.where, amount }; }
    if (mark) { query.where = { ...query.where, mark }; }
    if (time) { query.where = { ...query.where, time }; }
    const res = await ctx.model.Contract.findAll(query);
    const buffer = exportExcel(res);
    ctx.set('Content-Type', 'application/vnd.openxmlformats');
    ctx.set('Content-Disposition', 'attachment; filename=' + encodeURIComponent('文件名') + '.xlsx');
    ctx.set('Access-Control-Expose-Headers', 'Content-Disposition');
    ctx.body = buffer;
  }

  async destroy() {
    const ctx = this.ctx;
    const id = toInt(ctx.params.id);
    const delivery = await ctx.model.Contract.findByPk(id);
    if (!delivery) {
      ctx.body = {
        code: 1,
        data: [],
        message: '不存在的产品',
      };
      return;
    }

    await delivery.destroy();
    ctx.body = {
      code: 0,
      message: '删除成功',
    };
  }
}

module.exports = ContractController;
